import Vue from 'vue'
import MessageVue from './Message.vue'
const NoticeConstructor = Vue.extend(MessageVue)
let nid = 1
const Message = (options) => {
  let id = 'notice-' + nid ++
  options = options || {}
  if(typeof options === 'string'){
    options = {
      message: options
    }
  }
  const NoticeInstance = new NoticeConstructor({
    data: options
  })
  NoticeInstance.id = id
  NoticeInstance.vm = NoticeInstance.$mount()
  NoticeInstance.vm.visible = true
  NoticeInstance.dom = NoticeInstance.vm.$el
  document.body.appendChild(NoticeInstance.dom)
  NoticeInstance.dom.style.zIndex = nid + 1001
  return NoticeInstance.vm
}
['success','warning','info','error'].forEach(type => {
  Message[type] = options => {
    if(typeof options === 'string'){
      options = {
        message: options
      }
    }
    options.type= type
    return Message(options)
  }
})
export default Message


